//
//  StandingBook.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/1.
//

import Foundation
import HandyJSON

// MARK: - 电子台账
// MARK: - URL
let URLStandingBookDetail = "/standingBook/"                /// 获取详细信息
let URLStandingBookList = "/standingBook/list"             /// 台账历史记录列表
let URLStandingBookSave = "/standingBook/save"             /// 电子台账提交
let URLStandingBookSignout = "/standingBook/signout"       /// 签到离场
let URLStandingBookStatistics = "/standingBook/statistics" /// 统计
let URLStandingBookTypelist = "/standingBook/typelist"     /// 获取台账类型
let URLStandingBookUpload = "/standingBook/upload"         /// 上传图片
let URLStandingBookVisitor = "/standingBook/visitor"       /// 访客登记
let URLYjwxDztzSelectScaleByDept = "/yjwx/selectDztzScaleByDept" /// 根据部门查询电子台账完成率
let URLStandingBookSaveNotLogin = "/standingBook/saveNotLogin" /// 无出入人员
let URLStandingBookQueryHistory = "/standingBook/queryHistory" /// 历史草稿
let URLProfileUpload = "/profile/upload" /// 上传照片
let URLStandingBookQueryCompanyPage = "/standingBook/queryCompanyPage" /// 单位信息
let URLStandingBookQueryPeoplePage = "/standingBook/queryPeoplePage" /// 人员信息
let URLStandingBookQueryPeopleInfo = "/standingBook/queryPeopleInfo" /// 人员信息-详情
let URLStandingBookUpOrSaveInfo = "/standingBook/upOrSaveInfo" /// 人员信息-补充信息
let URLStandingBookQueryCheckHs = "/standingBook/queryCheckHs" /// 审批-检测详情
let URLStandingBookSendHs = "/standingBook/sendHs" /// 发起双人会同审批
let URLStandingBookChangeStatus = "/standingBook/changeStatus" /// 审批状态修改

struct StandingBookStaticParam: Encodable {
    var date: String?     // 时间 2022-11-25
    var deptId: Int64?    // 部门ID
    var dateType: Int?    // 日：1月：2季：3年：4
    var tzlx: String?     // bookType
    
    var timestamp: String?
    var signature: String?
        
    init(date: String? = nil, deptId: Int64? = nil, dateType: Int? = nil, tzlx: String? = nil) {
        self.date = date
        self.deptId = deptId
        self.dateType = dateType
        self.tzlx = tzlx
    }
}

struct StandingBookParam: Encodable {
    var date: String?     // 时间 2022-11-25
    var deptId: Int64?    // 部门ID
    var dateType: Int?    // 日：1月：2季：3年：4
    var bookId: Int64?    // 台账ID
    var bookType: String? // 台账类型
    var pageNum: Int?   // 页数
    var pageSize: Int?  // 每页条数
    
    var timestamp: String?
    var signature: String?
    
    init(date: String? = nil, deptId: Int64? = nil, dateType: Int? = nil, bookId: Int64? = nil, bookType: String? = nil, pageNum: Int? = nil, pageSize: Int? = nil) {
        self.date = date
        self.deptId = deptId
        self.dateType = dateType
        self.bookId = bookId
        self.bookType = bookType
        self.pageNum = pageNum
        self.pageSize = pageSize
    }
}

struct ElecParam: Encodable {
    var bookType: String?
    var tjrq: String?
    var attrValue0: String?
    var attrValue1: String?
    var attrValue2: String?
    var attrValue3: String?
    var attrValue4: String?
    var attrValue5: String?
    var attrValue6: String?
    var attrValue7: String?
    var attrValue8: String?
    var attrValue9: String?
    var attrValue10: String?
    var attrValue11: String?
    var attrValue12: String?
    var attrValue13: String?
    var attrValue14: String?
    var attrValue15: String?
    var attrValue16: String?
    var attrValue17: String?
    var attrValue18: String?
    var attrValue19: String?
    var attrValue20: String?
    var attrValue21: String?
    var attrValue22: String?
    var attrValue23: String?
    var attrValue24: String?
    var attrValue25: String?
    var attrValue26: String?
    var attrValue27: String?
    var attrValue28: String?
    var attrValue29: String?
    var attrValue30: String?
    var attrValue31: String?
    var attrValue32: String?
    var attrValue33: String?
    var attrValue34: String?
    var fjList: [FjdzParam]?
    var mode: String = "2"
    var hsId: String?
    
    var timestamp: String?
    var signature: String?

    init(bookType: String? = nil, tjrq: String? = nil, attrValue0: String? = nil, attrValue1: String? = nil, attrValue2: String? = nil, attrValue3: String? = nil, attrValue4: String? = nil, attrValue5: String? = nil, attrValue6: String? = nil, attrValue7: String? = nil, attrValue8: String? = nil, attrValue9: String? = nil, attrValue10: String? = nil, attrValue11: String? = nil, attrValue12: String? = nil, attrValue13: String? = nil, attrValue14: String? = nil, attrValue15: String? = nil, attrValue16: String? = nil, attrValue17: String? = nil, attrValue18: String? = nil, attrValue19: String? = nil, attrValue20: String? = nil, attrValue21: String? = nil, attrValue22: String? = nil, attrValue23: String? = nil, attrValue24: String? = nil, attrValue25: String? = nil, attrValue26: String? = nil, attrValue27: String? = nil, attrValue28: String? = nil, attrValue29: String? = nil, attrValue30: String? = nil, attrValue31: String? = nil, attrValue32: String? = nil, attrValue33: String? = nil, attrValue34: String? = nil, fjList: [FjdzParam]? = nil, mode: String = "2", hsId: String? = nil) {
        self.bookType = bookType
        self.tjrq = tjrq
        self.attrValue0 = attrValue0
        self.attrValue1 = attrValue1
        self.attrValue2 = attrValue2
        self.attrValue3 = attrValue3
        self.attrValue4 = attrValue4
        self.attrValue5 = attrValue5
        self.attrValue6 = attrValue6
        self.attrValue7 = attrValue7
        self.attrValue8 = attrValue8
        self.attrValue9 = attrValue9
        self.attrValue10 = attrValue10
        self.attrValue11 = attrValue11
        self.attrValue12 = attrValue12
        self.attrValue13 = attrValue13
        self.attrValue14 = attrValue14
        self.attrValue15 = attrValue15
        self.attrValue16 = attrValue16
        self.attrValue17 = attrValue17
        self.attrValue18 = attrValue18
        self.attrValue19 = attrValue19
        self.attrValue20 = attrValue20
        self.attrValue21 = attrValue21
        self.attrValue22 = attrValue22
        self.attrValue23 = attrValue23
        self.attrValue24 = attrValue24
        self.attrValue25 = attrValue25
        self.attrValue26 = attrValue26
        self.attrValue27 = attrValue27
        self.attrValue28 = attrValue28
        self.attrValue29 = attrValue29
        self.attrValue30 = attrValue30
        self.attrValue31 = attrValue31
        self.attrValue32 = attrValue32
        self.attrValue33 = attrValue33
        self.attrValue34 = attrValue34
        self.fjList = fjList
        self.mode = mode
        self.hsId = hsId
    }
}

struct StandingBookNotLoginParam: Encodable {
    var bootType: String? // 台账类型
    var deptId: String?
    
    var timestamp: String?
    var signature: String?
    
    init(bootType: String? = nil, deptId: String? = nil) {
        self.bootType = bootType
        self.deptId = deptId
    }
}

struct CompanyParam: Encodable {
    var pageNum: Int?
    var pageSize: Int?
    var gcsmc: String? // 公司名称
    
    var timestamp: String?
    var signature: String?
    
    init(pageNum: Int? = nil, pageSize: Int? = nil, gcsmc: String? = nil) {
        self.pageNum = pageNum
        self.pageSize = pageSize
        self.gcsmc = gcsmc
    }
}

struct PersonParam: Encodable {
    var pageNum: Int?
    var pageSize: Int?
    var nickName: String?
    var jobNum: String?
    var phonenumber: String?
    var userType: String? // 用户类型：00 行内人员 09 工程师 （技防、物防台账类型的人员列表必传） （默认：00）
    var companyId: String? // 单位主键ID userType 09 必填
    
    var timestamp: String?
    var signature: String?
    
    init(pageNum: Int? = nil, pageSize: Int? = nil, nickName: String? = nil, jobNum: String? = nil, phonenumber: String? = nil, userType: String? = nil, companyId: String? = nil) {
        self.pageNum = pageNum
        self.pageSize = pageSize
        self.nickName = nickName
        self.jobNum = jobNum
        self.phonenumber = phonenumber
        self.userType = userType
        self.companyId = companyId
    }
}

struct PersonInfoParam: Encodable {
    var userId: String?
    
    var timestamp: String?
    var signature: String?
    
    init(userId: String? = nil) {
        self.userId = userId
    }
}

struct StandingBookCommonParam: Encodable {
    var bookType: String? // 台账类型
    
    var timestamp: String?
    var signature: String?
    
    init(bookType: String? = nil) {
        self.bookType = bookType
    }
}

struct ChangeStatusParam: Encodable {
    var hsId: String? // 审批单主键ID
    var status: String? // 1 确认 2 取消 3 驳回
    
    var timestamp: String?
    var signature: String?
    
    init(hsId: String? = nil, status: String? = nil) {
        self.hsId = hsId
        self.status = status
    }
}

struct UpOrSaveInfoParam: Encodable {
    var userId: Int64? //主键ID
    var phonenumber: String? //手机号
    var bgdh: String?
    var nickName: String? //姓名
    var email: String? //
    var sex: String? // 性别 0=男,1=女,2=未知
    var zjzp: String? // 证件照片
    var bazfj: String? // 保安证或其他证件照片
    var idcard: String? // 证件号码
    var baidcard: String? // 保安证或其他证件号码
    var company: String? // 单位名称
    var companyId: String? // 单位主键ID
    var jobNum: String? // 工号
    
    var timestamp: String?
    var signature: String?
    
    init(userId: Int64? = nil, phonenumber: String? = nil, bgdh: String? = nil, nickName: String? = nil, email: String? = nil, sex: String? = nil, zjzp: String? = nil, bazfj: String? = nil, idcard: String? = nil, baidcard: String? = nil, company: String? = nil, companyId: String? = nil, jobNum: String? = nil) {
        self.userId = userId
        self.phonenumber = phonenumber
        self.bgdh = bgdh
        self.nickName = nickName
        self.email = email
        self.sex = sex
        self.zjzp = zjzp
        self.bazfj = bazfj
        self.idcard = idcard
        self.baidcard = baidcard
        self.company = company
        self.companyId = companyId
        self.jobNum = jobNum
    }
    
    init(withModal modal: PersonModal?) {
        self.userId = modal?.userId
        self.phonenumber = modal?.phonenumber
        self.bgdh = modal?.bgdh
        self.nickName = modal?.nickName
        self.email = modal?.email
        self.sex = modal?.sex
        self.zjzp = modal?.zjzp
        self.bazfj = modal?.bazfj
        self.idcard = modal?.idcard
        self.baidcard = modal?.baidcard
        self.company = modal?.company
        self.companyId = modal?.companyId
        self.jobNum = modal?.jobNum
    }
}

struct SendHsParam: Encodable {
    var tzlx: String?
    var tzlxmc: String?
    var jsrid: String?
    var jsrxm: String?
    
    var timestamp: String?
    var signature: String?
    
    init(tzlx: String? = nil, tzlxmc: String? = nil, jsrid: String? = nil, jsrxm: String? = nil) {
        self.tzlx = tzlx
        self.tzlxmc = tzlxmc
        self.jsrid = jsrid
        self.jsrxm = jsrxm
    }
}

// MARK: - API
extension API {
    /// 获取详细信息
    class func getStandingBookDetail(withId id: Int, block: ResponseBlock<StandingBookModal>?) {
        let param = getSign()
        NetManager.request(url: "\(URLStandingBookDetail)\(id)", method: .get, parameters: param, modelType: StandingBookModal.self, resultBlock: block)
    }
    
    /// 台账历史记录列表
    class func getStandingBookList(withParam _param: StandingBookParam, block: ResponseBlock<StandingBookModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookList, method: .get, parameters: param, modelType: StandingBookModal.self, resultBlock: block)
    }
    
    /// 历史草稿
    class func getStandingBookQueryHistory(withJson _param: StandingBookNotLoginParam, block: ResponseBlock<StandingBookHistoryModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookQueryHistory, method: .get, parameters: param, modelType: StandingBookHistoryModal.self, resultBlock: block)
    }
    
    /// 电子台账提交
    class func postStandingBookSave(withJson _param: CommonJsonParam, block: ResponseBlock<RespModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookSave, parameters: param, modelType: RespModal.self, resultBlock: block)
    }
    
    /// 无出入人员
    class func postStandingBookSaveNotLogin(withParam _param: StandingBookNotLoginParam, block: ResponseBlock<RespModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookSaveNotLogin, parameters: param, modelType: RespModal.self, resultBlock: block)
    }
    
    /// 签到离场
    class func postStandingBookSignout(withParam _param: StandingBookParam, block: ResponseBlock<StandingBookModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookSignout, parameters: param, modelType: StandingBookModal.self, resultBlock: block)
    }

    /// 统计
    class func getStandingBookStatistics(withParam _param: StandingBookParam, block: ResponseBlock<StandingBookStatisticsModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookStatistics, method: .get, parameters: param, modelType: StandingBookStatisticsModal.self, resultBlock: block)
    }
    
    /// 获取台账类型
    class func getStandingBookTypelist(withParam _param: StandingBookParam, block: ResponseBlock<StandingBookTypeModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookTypelist, method: .get, parameters: param, modelType: StandingBookTypeModal.self, resultBlock: block)
    }
    
    /// 上传图片
    class func postStandingBookUpload(withImage imagePath: String, block: ResponseBlock<ImageModal>?) {
        let param = getSign()
        NetManager.upload(url: URLStandingBookUpload, imagePath: imagePath, parameters: param, modelType: ImageModal.self, resultBlock: block)
    }
    
    /// 访客登记
    class func getStandingBookVisitor(withParam _param: StandingBookParam, block: ResponseBlock<StandingBookModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookVisitor, method: .get, parameters: param, modelType: StandingBookModal.self, resultBlock: block)
    }
    
    /// 根据部门查询电子台账完成率
    class func getDztzScaleByDept(withParam _param: StandingBookStaticParam, block: ResponseBlock<CommonScaleByDeptModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLYjwxDztzSelectScaleByDept, method: .get, parameters: param, modelType: CommonScaleByDeptModal.self, resultBlock: block)
    }
    
    /// 上传照片
    class func profileUpload(withImage imagePath: String, block: ResponseBlock<ImageModal>?) {
        let param = getSign()
        NetManager.upload(url: URLProfileUpload, imagePath: imagePath, parameters: param, modelType: ImageModal.self, resultBlock: block)
    }
    
    /// 单位信息
    class func getCompanys(withParam _param: CompanyParam, block: ResponseBlock<CompanyModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookQueryCompanyPage, method: .get, parameters: param, modelType: CompanyModal.self, resultBlock: block)
    }
    
    /// 人员信息
    class func getPeoples(withParam _param: PersonParam, block: ResponseBlock<PersonModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookQueryPeoplePage, method: .get, parameters: param, modelType: PersonModal.self, resultBlock: block)
    }
    
    /// 人员信息-详情
    class func getPeopleInfo(withParam _param: PersonInfoParam, block: ResponseBlock<PersonModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookQueryPeopleInfo, method: .get, parameters: param, modelType: PersonModal.self, resultBlock: block)
    }
    
    /// 人员信息-补充信息
    class func postUpOrSaveInfo(withParam _param: CommonJsonParam, block: ResponseBlock<RespModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookUpOrSaveInfo, parameters: param, modelType: RespModal.self, resultBlock: block)
    }
    
    /// 接收人 审批详情
    class func getCheckHs(withParam _param: StandingBookCommonParam, block: ResponseBlock<CheckHsModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookQueryCheckHs, method: .get, parameters: param, modelType: CheckHsModal.self, resultBlock: block)
    }
    
    /// 发起双人会同审批
    class func postSendHs(withParam _param: CommonJsonParam, block: ResponseBlock<RespModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookSendHs, parameters: param, modelType: RespModal.self, resultBlock: block)
    }
    
    /// 审批状态修改
    class func changeStatus(withParam _param: ChangeStatusParam, block: ResponseBlock<RespModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLStandingBookChangeStatus, parameters: param, modelType: RespModal.self, resultBlock: block)
    }
}

// MARK: - Modals
struct StandingBookHistoryModal: HandyJSON {
    var isHistory: Bool?
    var history: StandingBookModal?
}

struct StandingBookModal: HandyJSON {
    var bookId: Int64?
    var bookType: String?
    var tjrq: String?
    var status: String?
    var attrValue0: String?
    var attrValue1: String?
    var attrValue2: String?
    var attrValue3: String?
    var attrValue4: String?
    var attrValue5: String?
    var attrValue6: String?
    var attrValue7: String?
    var attrValue8: String?
    var attrValue9: String?
    var attrValue10: String?
    var attrValue11: String?
    var attrValue12: String?
    var attrValue13: String?
    var attrValue14: String?
    var attrValue15: String?
    var attrValue16: String?
    var attrValue17: String?
    var attrValue18: String?
    var attrValue19: String?
    var attrValue20: String?
    var attrValue21: String?
    var attrValue22: String?
    var attrValue23: String?
    var attrValue24: String?
    var attrValue25: String?
    var attrValue26: String?
    var attrValue27: String?
    var attrValue28: String?
    var attrValue29: String?
    var attrValue30: String?
    var attrValue31: String?
    var attrValue32: String?
    var attrValue33: String?
    var attrValue34: String?
    var deptName: String?
    var fjList: [StandingFjListModal]?
    var cjrq: String?
    var bdFlag: String?
}

struct StandingFjListModal: HandyJSON {
    var fjdz: String?
    var createBy: String?
    var createTime: String?
}

// 统计
struct StandingBookStatisticsModal: HandyJSON {
    var bdTotal: Int64?
    var percent: String?
    var unregTotal: Int64?
    var total: Int64?
    var complete: Int64?
}

// 获取台账类型
struct StandingBookTypeModal: HandyJSON {
    var typeLabel: String?
    var djbs: String?
//    var typeCode: Int64?
//    var typeSort: Int64?
    var typeValue: String?
    var remark: String?
    var status: String?
    var fpfs: String?
    var statusName: String?
}

struct CompanyModal: HandyJSON {
    var id: String? //  2, //主键ID
    var gcsmc: String? // "上海市保安服务（集团）有限公司", //公司名称
    var gcsfzr: String? // "曹斌", //负责人
    var gcsfzrlxdh: String? // "13817253343", //负责人手机号
    var gcsfwfcmc: String? // "监控设备故障,门禁设备故障,语音对讲设备故障,报警设备故障,消防设备故障", //服务范围
    var gcsfwfcdm: String? // "1,2,3,4,5", //服务范围代码
    var wbfwfcmc: String? // "技防维保服务,报警维保服务,消防巡检维保服务", //服务类型
    var wbfwfcdm: String? // "1,0,3" //服务类型代码
}

// 获取人员列表
struct PersonModal: HandyJSON {
    var userId: Int64? //主键ID
    var userType: String?
    var phonenumber: String? //手机号
    var bgdh: String?
    var userName: String?
    var nickName: String? //姓名
    var statusName: String?
    var email: String? //
    var sex: String? // 性别 0=男,1=女,2=未知
    var zjzp: String? // 证件照片
    var bazfj: String? // 保安证或其他证件照片
    var sfhy: String? // 是否合银
    var idcard: String? // 证件号码
    var baidcard: String? // 保安证或其他证件号码
    var company: String? // 单位名称
    var companyId: String? // 单位主键ID
    var jobNum: String? // 工号
}

struct CheckHsModal: HandyJSON {
    var status: String? //状态：0 无\可发起审批  1 接收人详情  2 发起人详情
    var info: CheckHsInfoModal?
}

struct CheckHsInfoModal: HandyJSON {
    var remark: String? // null,
    var hsId: Int64? // 200, //主键ID
    var fqrid: Int64? // 1005, //发起人
    var fqrxm: String? // "分行-王五", //发起人姓名
    var jsrid: Int64? // 1007, //接收人ID
    var jsrxm: String? // "ces", //接收人姓名
    var tzlx: String? // "I", //台账类型
    var tzlxmc: String? // "接、送库款箱记录簿", //台账类型名称
    var status: String? // "0" //0 未处理  1 确认  2 驳回 3 取消
}
